package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.sqfw.models.Sqfw_po_user;
import com.budwk.app.sqfw.services.SqfwPoUserService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/sqfw/po/user")
@ApiDefinition(tag = "泡泡用户映射表")
@Slf4j
public class SqfwPoUserController{
    @Inject
    private SqfwPoUserService sqfwPoUserService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询泡泡用户映射表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Sqfw_po_user.class
    )
    @ApiResponses(
    implementation = Sqfw_po_user.class
    )
    @SaCheckPermission("sqfw.po.user")
    public Result<?> list(@Param("..")Sqfw_po_user bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getSys_userid() !=null && Strings.isNotBlank(bean.getSys_userid().toString())) {
            cnd.and("sys_user","=",bean.getSys_userid());
        }
        
        if (bean.getPo_userid() !=null && Strings.isNotBlank(bean.getPo_userid().toString())) {
            cnd.and("po_userid","=",bean.getPo_userid());
        }
        
        return Result.data(sqfwPoUserService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增泡泡用户映射表")
    @ApiFormParams(
        implementation = Sqfw_po_user.class
    )
    @ApiResponses
    @SLog(tag = "新增泡泡用户映射表", value = "${args[0]}")
    @SaCheckPermission("sqfw.po.user.create")
    public Result<?> create(@Param("..")Sqfw_po_user sqfwPoUser, HttpServletRequest req) {
        sqfwPoUser.setCreatedBy(SecurityUtil.getUserId());
        sqfwPoUserService.insert(sqfwPoUser);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改泡泡用户映射表")
    @ApiFormParams(
        implementation = Sqfw_po_user.class
    )
    @ApiResponses
    @SLog(tag = "修改泡泡用户映射表", value = "${args[0].id}")
    @SaCheckPermission("sqfw.po.user.update")
    public Result<?> update(@Param("..")Sqfw_po_user sqfwPoUser, HttpServletRequest req) {
        sqfwPoUser.setUpdatedBy(SecurityUtil.getUserId());
        sqfwPoUserService.updateIgnoreNull(sqfwPoUser);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取泡泡用户映射表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除泡泡用户映射表", value = "${args[0]}")
    @SaCheckPermission("sqfw.po.user")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Sqfw_po_user sqfwPoUser = sqfwPoUserService.fetch(id);
        if (sqfwPoUser == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(sqfwPoUser);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除泡泡用户映射表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除泡泡用户映射表", value = "${args[0]}")
    @SaCheckPermission("sqfw.po.user.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Sqfw_po_user sqfwPoUser = sqfwPoUserService.fetch(id);
        if (sqfwPoUser == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwPoUserService.delete(id);
        req.setAttribute("_slog_msg", sqfwPoUser.getId());
        return Result.success();
    }
}
